package com.baizhi.controller;

import com.baizhi.entity.Emp;
import com.baizhi.service.EmpService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FilenameUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * @Auther: dlh
 * @Date: 2020/11/19
 * @Descible:${name}
 * @Version:1.0
 */
@RestController
@RequestMapping("emp")
@CrossOrigin
@Slf4j
public class EmpController {

    @Autowired
    private EmpService empService;

    @Value("${photo.dir}")
    private String realPath;


    /**
     * 更新员工的方法
     */
    @PostMapping("update")
    public Map<String, Object> update(Emp emp, MultipartFile photo) throws IOException {
        log.info("员工信息:[{}]", emp.toString());
        Map<String, Object> map = new HashMap<String, Object>();
        try {
            if (photo != null && photo.getSize() != 0) {
                log.info("图片信息:[{}]", photo.getOriginalFilename());
                //头像保存
                String newFileName = UUID.randomUUID().toString() + "." + FilenameUtils.getExtension(photo.getOriginalFilename());
                photo.transferTo(new File(realPath, newFileName));
                //设置头像地址
                emp.setPath(newFileName);
            }
            //保存信息
            empService.update(emp);
            map.put("state", true);
            map.put("msg", "提示:保存成功");
        } catch (Exception e) {
            e.printStackTrace();
            map.put("state", false);
            map.put("msg", "提示:保存失败" + e.getMessage());
        }
        return map;

    }

    /**
     * 删除员工的方法
     */
    @GetMapping("delete")
    public Map<String, Object> delete(String id) {
        log.info("删除信息:[{}]", id);
        Map<String, Object> map = new HashMap<String, Object>();
        try {
            //删除头像
            Emp emp = empService.findById(id);
            File file = new File(realPath, emp.getPath());
            if (file.exists()) file.delete();
            //删除员工
            empService.delete(id);
            map.put("state", true);
            map.put("msg", "删除员工成功");
        } catch (Exception e) {
            e.printStackTrace();
            map.put("state", false);
            map.put("msg", "删除员工错误" + e.getMessage());
        }
        return map;
    }

    /**
     * 保存员工的方法
     */
    @PostMapping("save")
    public Map<String, Object> save(Emp emp, MultipartFile photo) throws IOException {
        log.info("员工信息:[{}]", emp.toString());
        log.info("图片信息:[{}]", photo.getOriginalFilename());
        Map<String, Object> map = new HashMap<String, Object>();
        try {
            //头像保存
            String newFileName = UUID.randomUUID().toString() + "." + FilenameUtils.getExtension(photo.getOriginalFilename());
            photo.transferTo(new File(realPath, newFileName));
            //设置头像地址
            emp.setPath(newFileName);
            //保存信息
            empService.save(emp);
            map.put("state", true);
            map.put("msg", "提示:保存成功");
        } catch (Exception e) {
            e.printStackTrace();
            map.put("state", false);
            map.put("msg", "提示:保存失败" + e.getMessage());
        }
        return map;

    }

    /**
     * 查询所有员工的方法
     */
    @GetMapping("findAll")
    private List<Emp> findAll() {
        return empService.findAll();
    }

    /**
     * 查询所有员工ID的方法
     */
    @GetMapping("findById")
    private Emp findById(String id) {
        return empService.findById(id);
    }
}
